// pages/schedule_list/index.js
import { getScheduleList } from '../../api/schedule.js'
import { removeObj } from '../../utils/removeObj.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists: [],
    pageNum: 1,
    pageSize: 6,
    isRequst: false,
    showToastState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  onShow: function () {
    this.feach()
  },
  feach() {
    this.setData({ pageNum: 1,lists:[], isRequst: false, showToastState: false })
    this._getScheduleList()
  },
  // 获取赛程列表
  _getScheduleList: function () {
    if (!this.data.isRequst) {
      this.setData({ isRequst: true })
      let { pageNum, pageSize, lists } = this.data
      getScheduleList({
        pageNum: pageNum,
        pageSize: pageSize
      },true).then(res => {
        this.setData({ isRequst: false})
        let arr = res.rows;
        if (arr.length > 0) {
          arr.forEach(e=>{
            if (e.timeStart != '' && e.timeStart != null) {
              let day = app.timeFn(e.timeStart, '-', false)
              e.date = Number((day[4] ? '-' : '') + day[0])
              e.day = e.day.replace(/-/g, '/')
              e.shortAddress = e.shortAddress.replace(/,/g, '/')
            }
          })
          this.setData({ lists: [...(pageNum == 1 ? [] : lists), ...removeObj(arr, ['searchValue', 'createBy', 'createTime', 'remark', 'updateBy', 'updateTime', 'params', 'timeEnd', 'time'])], pageNum: pageNum + 1 })
          this.setData({ showToastState: this.data.lists.length == res.total })
        } else {
          this.setData({ showToastState: true })
        }
      })
    }
  },
  //进去赛程详情
  detail(e) {
    let d = e.currentTarget.dataset
    if (d.matchid != 0) {
      wx.navigateTo({
        url: '/pages/competition_detail/index?id=' + d.matchid,
      })
    } else {
      wx.navigateTo({
        url: "/pages/schedule_detail/index?id=" + d.id,
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.feach()
  },
  onReachBottom: function () {
    this.setData({isRequst: false});
    this._getScheduleList();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})